package com.yanqu.road.server.manger.activity.ruishou;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ruishou.RuiShouConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.RuiShouBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ruishou.RuiShouModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

public class RuiShouMgr extends TempMgr {

    private static RuiShouConfig config;

    public static RuiShouConfig getConfig() {
        return config;
    }

    /**
     * 是否积分道具
     *
     * @param goodsId 道具id
     */
    public static boolean isRuiShouScoreItemId(int goodsId) {
        if (config == null) {
            return false;
        }
        return config.getRuiShouScoreItemId() == goodsId;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload RuiShou Activity start");

        ActivityInfo activityInfo;

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.RuiShou.getValue());
        if (openActivityInfoList.isEmpty() || openActivityInfoList.get(0) == null) {
            getLogger().info("no RuiShou Activity in show time");
            config = null;
            return;
        } else {
            activityInfo = openActivityInfoList.get(0);
        }
        RuiShouConfig tmpConfig = new RuiShouConfig();
        tmpConfig.setActivityInfo(activityInfo);
        int tmpActivityId = activityInfo.getActivityId();
        // config配置
        tmpConfig.setAreaConfigMap(RuiShouBussiness.getRuiShouAreaConfigMap(tmpActivityId));
        tmpConfig.setMonsterConfigMap(RuiShouBussiness.getRuiShouMonsterConfigMap(tmpActivityId));
        tmpConfig.setActionConfigMap(RuiShouBussiness.getRuiShouActionConfigMap(tmpActivityId));
        tmpConfig.setActionTenConfigMap(RuiShouBussiness.getRuiShouActionTenConfig(tmpActivityId));
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
        if (activityConfigMap.containsKey("EXCHANGE_SCORE_ITEM_ID")) {
            tmpConfig.setRuiShouScoreItemId(activityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue());
        }
        if (activityConfigMap.containsKey("XIANGRUI_ITEM2")) {
            tmpConfig.setRuiShouCostItemId(activityConfigMap.get("XIANGRUI_ITEM2").getIntValue());
        }
        if (activityConfigMap.containsKey("XIANGRUI_TEN_EXTRACT_OPEN")) {
            tmpConfig.setTenExtractOpen(activityConfigMap.get("XIANGRUI_TEN_EXTRACT_OPEN").getIntValue());
        }
        if (activityConfigMap.containsKey("XIANGRUI_TEN_EXTRACT_CONSUME")) {
            tmpConfig.setTenExtractConsume(activityConfigMap.get("XIANGRUI_TEN_EXTRACT_CONSUME").getIntValue());
        }
        if (activityConfigMap.containsKey("XIANGRUI_MONSTER_HP")) {
            tmpConfig.setMonsterHp(activityConfigMap.get("XIANGRUI_MONSTER_HP").getIntValue());
        }
        config = tmpConfig;

        getLogger().info("reload RuiShou Activity end");

        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            gamePlayer.getModule(RuiShouModule.class).initSystem();
        }
    }
}
